﻿using System;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Diagnostics;

namespace wscf_lightblue_VSAddIn
{
	/// <summary>
	/// Builds add in menu items
	/// </summary>
	public class MenuBuilder
	{
		private readonly VisualStudio visualStudio;
		private readonly SelectionEvents selectionEvents;
		private readonly CommandBar subMenuCommandBar;
		private readonly CommandBar webSubMenuCommandBar;
		private readonly CommandBar projectSubMenuCommandBar;
		private readonly CommandBar webProjectSubMenuCommandBar;
		private readonly CommandBar toolsSubMenuCommandBar;
		private readonly CommandBarPopup subMenuPopup;
		private readonly CommandBarPopup webSubMenuPopup;
		private readonly CommandBarPopup projectSubMenuPopup;
		private readonly CommandBarPopup webProjectSubMenuPopup;
		private readonly CommandBarPopup toolsSubMenuPopup;

		/// <summary>
		/// Command names 
		/// </summary>
		public const string GENERATE_CODE_DIALOG_COMMAND_NAME = "GenCodeDlg";
		public const string GENERATE_WSDL_DIALOG_COMMAND_NAME = "GenWsdlDlg";
		public const string CREATE_XML_SCHEMA = "CreateXmlSchema";

		/// <summary>
		/// Initializes a new instance of the <see cref="MenuBuilder"/> class.
		/// </summary>
		/// <param name="visualStudio">The visual studio.</param>
		public MenuBuilder(VisualStudio visualStudio)
		{
			this.visualStudio = visualStudio;

			try
			{
				CommandBars commandBars = (CommandBars)visualStudio.ApplicationObject.CommandBars;
				CommandBar itemCommandBar = commandBars["Item"];
				CommandBar webItemCommandBar = commandBars["Web Item"];
				CommandBar projectCommandBar = commandBars["Project"];
				CommandBar webProjectCommandBar = commandBars["Web Project Folder"];
				CommandBar toolsCommandBar = commandBars["Tools"];

				subMenuPopup = (CommandBarPopup)itemCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
				subMenuPopup.Caption = "wscf.lightblue";
				subMenuCommandBar = subMenuPopup.CommandBar;

				webSubMenuPopup = (CommandBarPopup)webItemCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
				webSubMenuPopup.Caption = "wscf.lightblue";
				webSubMenuCommandBar = webSubMenuPopup.CommandBar;

				projectSubMenuPopup = (CommandBarPopup)projectCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
				projectSubMenuPopup.Caption = "wscf.lightblue";
				projectSubMenuCommandBar = projectSubMenuPopup.CommandBar;

				webProjectSubMenuPopup = (CommandBarPopup)webProjectCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
				webProjectSubMenuPopup.Caption = "wscf.lightblue";
				webProjectSubMenuCommandBar = webProjectSubMenuPopup.CommandBar;

				toolsSubMenuPopup = (CommandBarPopup)toolsCommandBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, 1, true);
				toolsSubMenuPopup.Caption = "wscf.lightblue";
				toolsSubMenuCommandBar = toolsSubMenuPopup.CommandBar;
			}
			catch (Exception exc)
			{
				EventLog.WriteEntry("wscf.lightblue", exc.ToString(), EventLogEntryType.Error);
			}
		}

		/// <summary>
		/// Adds the command bar to tools menu.
		/// </summary>
		/// <param name="addInInstance">The add in instance.</param>
		/// <param name="applicationObject">The application object.</param>
		/// <param name="commands">The commands.</param>
		/// <param name="contextGUIDS">The context GUIDS.</param>
		/// <param name="commandBars">The command bars.</param>
		/// <returns></returns>
		public object[] AddCommandBarToToolsMenu(AddIn addInInstance, DTE2 applicationObject, Commands2 commands, object[] contextGUIDS, CommandBars commandBars)
		{
			Command[] newCommands = new Command[]
			{
			commands.AddNamedCommand2(
					addInInstance,
					GENERATE_CODE_DIALOG_COMMAND_NAME,
					"Generate WCF Code from contract",
					"Generates WCF client proxy, service Interface and data contracts from WSDL or XSD files",
					true),
			commands.AddNamedCommand2(
					addInInstance,
					GENERATE_WSDL_DIALOG_COMMAND_NAME,
					"Generate WSDL from Schema",
					"Generates WSDL Service Description file from XSD files",
					true),
			commands.AddNamedCommand2(
					addInInstance,
					CREATE_XML_SCHEMA,
					"Create XML Schema",
					"Create HelloWorld Service XML Schema File (XSD)",
					true)
			};

			int position = 1;
			foreach (Command command in newCommands)
			{
				command.AddControl(toolsSubMenuCommandBar, position);
				command.AddControl(projectSubMenuCommandBar, position);
				command.AddControl(webProjectSubMenuCommandBar, position);
				command.AddControl(subMenuCommandBar, position);
				command.AddControl(webSubMenuCommandBar, position);
				position++;
			}

			return contextGUIDS;
		}

		public void CleanUp(string typeName)
		{
			RemoveCommandBarPopup(subMenuPopup);
			RemoveCommandBarPopup(webSubMenuPopup);
			RemoveCommandBarPopup(projectSubMenuPopup);
			RemoveCommandBarPopup(webProjectSubMenuPopup);
			RemoveCommandBarPopup(toolsSubMenuPopup);

			RemoveCommand(typeName + "." + GENERATE_CODE_DIALOG_COMMAND_NAME);
			RemoveCommand(typeName + "." + GENERATE_WSDL_DIALOG_COMMAND_NAME);
			RemoveCommand(typeName + "." + CREATE_XML_SCHEMA);
		}

		private void RemoveCommand(string commandName)
		{
			Command command = null;
			try
			{
				command = visualStudio.ApplicationObject.Commands.Item(commandName, -1);
			}
			catch
			{
			}

			if (command != null)
			{
				command.Delete();
			}
		}

		private void RemoveCommandBarPopup(CommandBarPopup commandBarPopup)
		{
			if (commandBarPopup != null)
			{
				commandBarPopup.Delete(true);
			}
		}

	}
}
